<template>
<!-- 加盟商-加盟商列表 -->
  <div>
    <Card :bordered="false" dis-hover class="ivu-mt tablebox" :padding="16">
      <div class="new_tab">
        <!-- Tab栏切换 -->
        <Tabs @on-click="onClickTab">
          <TabPane label="全部" name="all" />
          <TabPane label="营业中" name="1" />
          <!-- <TabPane label="休息中" name="0"/> -->
          <TabPane label="已停业" name="-1" />
        </Tabs>
      </div>
	    <Button type="primary" @click="add">添加加盟商</Button>
      <div class="table">
        <Table
          :columns="columns"  
          :data="orderList"
          ref="table"
          class="ivu-mt"
          :loading="loading"
          highlight-row
          no-userFrom-text="暂无数据"
          no-filtered-userFrom-text="暂无筛选结果"
        >
          <template slot-scope="{ row }" slot="image">
            <img :src="row.image" />
          </template>
          <template slot-scope="{ row, index }" slot="action">
            <a @click="gostore(row)">进入加盟商</a>
			
			 <Divider type="vertical" />
			<a @click="menuSelect(4,row.id)">充值</a>
			
			
			<Divider type="vertical" />
			<a @click="giveLevel(row.id)">赠送等级</a>
			
			
			
			
            <Divider type="vertical" />
            <a @click="reset(row)">重置</a>
            <Divider type="vertical" />
            <a @click="operation(row)">{{
              row.is_show == 0 ? "开业" : "停业"
            }}</a>
            <Divider type="vertical" />
            <a @click="edit(row)">编辑</a>
            <Divider type="vertical" />
            <a @click="delte(row, '删除加盟商（同步删除商品）', index)">删除</a>
          </template>
        </Table>
        <div class="acea-row row-right page">
          <Page
            :total="total"
            :current="formValidate.page"
            show-elevator
            show-total
            @on-change="pageChange"
            :page-size="formValidate.limit"
          />
        </div>
      </div>
    </Card>
	
	
	
	
	<!-- 编辑表单 积分余额-->
	<edit-from
	    ref="edits"
	    :FromData="FromData"
	    :userEdit="1"
	    @submitFail="submitFail"
	></edit-from>
	
	
	
	
	
	<!-- 批量设置 -->
	<Modal v-model="batchModal" title="批量设置" width="750" class-name="batch-modal" @on-visible-change="batchVisibleChange">
	 <!-- <Alert show-icon>每次只能修改一项，如需修改多项，请多次操作。</Alert> -->
	  <Row type="flex" align="middle">
	    <Col span="20">
	      <Form :model="batchData" :label-width="122">
	        
	        <FormItem  label="修改余额：">
	          <RadioGroup v-model="batchData.money_status">
	            <Radio :label="1">增加</Radio>
	            <Radio :label="2">减少</Radio>
	          </RadioGroup>
	        </FormItem>
	        <FormItem  label="余额：">
	          <InputNumber v-model="batchData.money" :min="0" :max="999999"></InputNumber>
	        </FormItem>
	       
	      </Form>
	    </Col>
	  </Row>
	  <div slot="footer">
	    <Button @click="cancelBatch">取消</Button>
	    <Button type="primary" @click="saveBatch">保存</Button>
	  </div>
	</Modal>
	
	
	
	
    <add-store ref="template"></add-store>
  </div>
</template>

<script>
import util from "@/libs/util";
import Setting from "@/setting";
import addStore from "../components/addStore";
import editFrom from "../../../components/from/from";
// import {
//   userList,
//   getUserData,
//   isShowApi,
//   editOtherApi,
//   giveLevelApi,
//   userSetGroup,
//   userGroupApi,
//   levelListApi,
//   userSetLabelApi,
//   userLabelApi,
//   userSynchro,
//   getUserSaveForm,
//   giveLevelTimeApi,
//   extendInfo,
//   batchProcess
// } from "@/api/user";



import {
  storeListApi,
  storeLogin,
  storeSetShowApi,
  resetApi,
  batchProcess,
  giveLevelApi
} from "@/api/store";
export default {
  name: "storeList",
  components: {
    addStore,
	  editFrom,
  },
  data() {
    return {
      BaseURL: '',
      total: 0,
      loading: false,
      formValidate: {
        type: "all",
        page: 1,
        limit: 15,
      },
      columns: [
        {
          title: "ID",
          key: "id",
          width: 60,
        },
        {
          title: "加盟商图片",
          slot: "image",
          minWidth: 80,
        },
        {
          title: "加盟商名称",
          key: "name",
          minWidth: 80,
        },
        {
          title: "加盟商分类",
          key: "cate_name",
          minWidth: 80,
        },
        {
          title: "联系电话",
          key: "phone",
          minWidth: 90,
        },
        {
          title: "加盟商地址",
          key: "address",
          ellipsis: true,
          minWidth: 150,
        },
		
		{
		  title: "用户uid",
		  key: "uid",
		  ellipsis: true,
		  minWidth: 150,
		},
		
		{
		  title: "加盟商余额",
		  key: "now_money",
		  ellipsis: true,
		  minWidth: 150,
		},
		
		{
		  title: "加盟商等级",
		  key: "level_name",
		  ellipsis: true,
		  minWidth: 150,
		},
		
        {
          title: "营业时间",
          key: "day_time",
          minWidth: 120,
        },
        {
          title: "营业状态",
          key: "status_name",
          minWidth: 80,
        },
        {
          title: "操作",
          slot: "action",
          fixed: "right",
          minWidth: 240,
          align: "center",
        },
      ],
      orderList: [
        {
          id: "1",
          order_id: "12",
          pay_price: "12",
          status: 1,
          phone: "13000000000",
          address: "陕西省西安市莲湖区大兴西路啊餐厨",
        },
      ],
	  
	  //新增参数处理
	   batchModal: false,
	     menuActive: 1,
		 batchData: {
		   group_id: 0,
		   label_id: [],
		   level_id: 0,
		   money_status: 0,
		   money: 0,
		   integration_status: 0,
		   integration: 0,
		   days_status: 1,
		   day: 0,
		   spread_uid: '',
		 },
		 storeId:'',
		   //新增参数处理
		 FromData: null,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
	  //赠送等级处理
	  // giveLevel(id){
		 //  this.giveLevel(id);  
	  // },
	  // 赠送会员等级
	  giveLevel(id) {
		  // alert(id);
	    giveLevelApi(id)
	        .then(async (res) => {
	          if (res.data.status === false) {
	            return this.$authLapse(res.data);
	          }
	          this.FromData = res.data;
	          this.$refs.edits.modals = true;
	        })
	        .catch((res) => {
	          this.$Message.error(res.msg);
	        });
	  },
	  // 修改成功
	  submitFail(p) {
	    // this.getList();
	    if (this.$refs.userDetails.modals) {
	      this.$refs.userDetails.getDetails(this.userId);
	    }
	  },
	   // 修改成功
	  cancelBatch() {
	    this.batchModal = false;
	  },
	  // 保存批量操作
	  saveBatch() {
	    batchProcess({
	      type: this.menuActive,
	      // uids: this.checkUidList,
		  storeId:this.storeId,
	      all: this.isAll,
	      where: this.userFrom,
	      data: this.batchData
	    }).then(res => {
	      this.$Message.success(res.msg);
	      this.batchModal = false;
	    }).catch(res => {
	      this.$Message.error(res.msg);
	    });
	  },
	  //新增开始
	  batchVisibleChange() {
	    this.batchData = {
	      group_id: 0,
	      label_id: [],
	      level_id: 0,
	      money_status: 0,
	      money: 0,
	      integration_status: 0,
	      integration: 0,
	      days_status: 1,
	      day: 0,
	      spread_uid: '',
	    };
	    this.batchLabel = [];
	    this.spread_name = '';
	    this.menuActive = 1;
	  },
	  //充值处理
	  menuSelect(name,storeId) {
		// alert(name);
	    this.menuActive = name;
		this.storeId=storeId;
		this.batchModal=true;
	  },
	  //新增结束
    getList() {
      this.loading = true;
      storeListApi(this.formValidate).then((res) => {
        this.orderList = res.data.list;
        this.total = res.data.count;
        this.loading = false;
      });
    },
    reset(row) {
      this.$modalForm(resetApi(row.id)).then(() => this.getList());
    },
    add() {
	  this.$refs.template.clearFrom();
      this.$refs.template.title = "添加加盟商";
      this.$refs.template.add = 1;
      this.$refs.template.isTemplate = true;
      this.$refs.template.cityInfo({ pid: 0 });
    },
    edit(row) {
      this.$refs.template.title = "编辑加盟商";
      this.$refs.template.isTemplate = true;
      this.$refs.template.getInfo(row.id);
    },
    getExpiresTime(expiresTime) {
      let nowTimeNum = Math.round(new Date() / 1000);
      let expiresTimeNum = expiresTime - nowTimeNum;
      return parseFloat(parseFloat(parseFloat(expiresTimeNum / 60) / 60) / 24);
    },
	
    // 进入加盟商
    gostore(item) {
      storeLogin(item.id)
        .then((res) => {
          let data = res.data;
          let expires = data.expires_time;
          util.cookies.setStore("token", data.token, {
            expires: expires,
          });
          util.cookies.setStore("uuid", data.user_info.id, {
            expires: expires,
          });
          util.cookies.setStore("expires_time", expires, {
            expires: expires,
          });
          util.makeMenu(`/${data.prefix}`, data.menus);
          let storage = window.localStorage;
          storage.setItem("menuListStore", JSON.stringify(data.menus));
          storage.setItem("uniqueAuthStore", JSON.stringify(data.unique_auth));
          let userInfoStore = {
            account: data.user_info.account,
            head_pic: data.user_info.avatar,
            logo: data.logo,
            logoSmall: data.logo_square,
            version: data.version,
          };
          storage.setItem("userInfoStore", JSON.stringify(userInfoStore));
          // menuListStore
          this.BaseURL = Setting.apiBaseURL.replace(/adminapi/, `${item.prefix}/home/`);
          window.open(this.BaseURL);
        })
        .catch((err) => {
          this.$Message.error(err.msg);
        });
    },
    delte(row, tit, num) {
      let delfromData = {
        title: tit,
        num: num,
        url: `store/store/del/${row.id}`,
        method: "DELETE",
        ids: "",
      };
      this.$modalSure(delfromData)
        .then((res) => {
          this.$Message.success(res.msg);
          this.orderList.splice(num, 1);
          if (!this.orderList.length) {
            this.formValidate.page =
                this.formValidate.page == 1 ? 1 : this.formValidate.page - 1;
          }
          this.getList();
        })
        .catch((res) => {
          this.$Message.error(res.msg);
        });
    },
    operation(row) {
      let a = 0; //修改营业状态的反值
      if (row.is_show == 0) {
        a = 1;
      }
      if (row.is_show == 1) {
        a = 0;
      }
      storeSetShowApi(row.id, a)
        .then((res) => {
          this.getList();
          this.$Message.success(res.msg);
        })
        .catch((err) => {
          this.$Message.error(res.msg);
        });
    },
    searchs() {},
    onClickTab(e) {
      this.formValidate.page = 1;
      this.formValidate.type = e;
      this.getList();
    },
    //分页
    pageChange(status) {
      this.formValidate.page = status;
      this.getList();
    },
  },
};
</script>

<style scoped lang="stylus">
/deep/.ivu-tabs-nav {
  height: 45px;
}
.tablebox {
  margin-top: 15px;
}
.btnbox {
  padding: 20px 0px 0px 30px;
  .btns {
    width: 99px;
    height: 32px;
    background: #1890ff;
    border-radius: 4px;
    text-align: center;
    line-height: 32px;
    color: #ffffff;
    cursor: pointer;
  }
}
.table {
  padding: 0;
  img {
    width: 40px;
    height: 40px;
  }
}
.search {
  width: 86px;
  height: 32px;
  background: #1890ff;
  border-radius: 4px;
  text-align: center;
  line-height: 32px;
  font-size: 13px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  cursor: pointer;
}
.reset {
  width: 86px;
  height: 32px;
  border-radius: 4px;
  border: 1px solid rgba(151, 151, 151, 0.36);
  text-align: center;
  line-height: 32px;
  font-size: 13px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  cursor: pointer;
}
.new_tab {
    >>>.ivu-tabs-nav .ivu-tabs-tab{
        padding:4px 16px 20px !important;
        font-weight: 500;
    }
  }
</style>
